package eBrain.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import eBrain.beans.Bid;
import eBrain.beans.CategorySales;
import eBrain.beans.Item;
import eBrain.beans.Sales;
import eBrain.beans.User;

public class SalesWorker {
	
	private Item item = new Item();
	private static User user = new User();
	private static DataManager dataManager = null;
	private static ArrayList<Item> items;
	
	
	public SalesWorker( User user, DataManager datamanager){
		this.user = user;
		this.dataManager = datamanager;
	}
	
	public SalesWorker( User user, DataManager dataManager, ArrayList<Item> items){
		this.user = user;
		this.dataManager = dataManager;
		this.items = items;
		
	}
		public static Sales getSales() {
			 ArrayList salesArray = dataManager.getItemByUserID(user.getUser_id());
			 Sales sales = new Sales();  
			    Iterator iterator  = salesArray.iterator();
			    	
			 		while(iterator.hasNext()){
			 			Item item = (Item)iterator.next();
			 			Bid bid = dataManager.getMaxBidForItem(item.getId());
			 			if(item.getEnd().after(new Date())){
			 				sales.setRunnigSales(sales.getRunnigSales()+bid.getBidValue());
			 				sales.setCountRunnig(sales.getCountRunnig()+1);
			 			}
			 			else {
			 				sales.setFinishedSales(sales.getFinishedSales()+bid.getBidValue());
			 				sales.setCountFinished(sales.getCountFinished()+1);
			 				sales.setFees(sales.getFees()+(bid.getBidValue()*0.03));
			 				
			 			}
			 		 	sales.setSales(sales.getSales()+bid.getBidValue());
			    	}
			
			return sales;
	
		}
		
		public static CategorySales getSalesForCategory(){
			double categorySalesSum = 0;
			double categoryFeeSum = 0;
			
	        Iterator<Item> iterator = items.iterator();
	        while (iterator.hasNext()) {
	          Item catitem = (Item)iterator.next();
	          
	          if((new Date(System.currentTimeMillis()).after(catitem.getEnd()))){
	        	  if(dataManager.hasBid(catitem.getId())==true){
		        	  if(!(catitem.getStartBid()==(dataManager.getMaxBidForItem(catitem.getId()).getBidValue()))){
		        		 double bidvalue = dataManager.getMaxBidForItem(catitem.getId()).getBidValue();  
		        		 categorySalesSum += bidvalue;
		        		 categoryFeeSum += bidvalue*0.05;
		        	  }
	        	  }
	          }
			}
	        CategorySales categorySales = new CategorySales();
	        categorySales.setCategorySalesFees(categoryFeeSum);
	        categorySales.setCategorySalesSum(categorySalesSum);  
	        return categorySales;
		}
    }


